home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 0769 / dsplist.c next >
C/C++ Source or Header  |  1997-04-10  |  4KB  |  169 lines

  1. //
  2. // Finger Version 3.1, a Windows Sockets Finger Client
  3. //
  4. // Copyright 1992, 1993 Network Research Corporation
  5. //
  6. // Permission to use, modify, and distribute this software and its
  7. // documentation for any purpose and without fee is hereby granted, provided
  8. // that the above copyright notice appears in all copies and that both
  9. // that copyright notice and this permission notice appear in supporting
  10. // documentation.  NRC makes no claims as to the suitability of this software
  11. // for any purpose.
  12. //
  13. // Module DSPLIST allows the NETWRK_ module to build a display list,
  14. // and the FINGER module to retrieve one.  A display list is a linked
  15. // list of text lines accompanied by a count of list entries.  The finger
  16. // client uses display lists to represent the the remote finger server's
  17. // return data in a form suitable for scrolling window display.
  18. //
  19. // 10/20/92  Lee Murach    Created.
  20. // 12/02/92  Lee Murach    PushChars() replaces PushChar() as export, LF
  21. //                         triggers newline, CRs discarded
  22. //
  23.  
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <windows.h>
  27. #include "finger.h"
  28.  
  29. #define  LF 10                         // linefeed
  30. #define  CR 13                         // carriage return
  31. #define  TABSTRING "        "          // 8 character tab
  32.  
  33. VOID PushChar(char ch);
  34. VOID PushLine(VOID);
  35. VOID FreeLine(LINEITEM *pLine);
  36.  
  37. char LineBuf[132];                     // holds accumulating text line
  38. int LineLen;                           // length of text line
  39. LINEITEM *pLast;                       // last item on list
  40. LINEITEM *pFirst;                      // root of line list
  41. int LineCount;                         // n items in list
  42.  
  43. //
  44. // PushChars -- pushes buffer of characters into the display list;
  45. // expands tabs.
  46. //
  47. VOID PushChars(char *buf, int buflen)
  48. {
  49.    while (buflen--)
  50.    {
  51.       if (*buf == '\t')
  52.          PushChars(TABSTRING, strlen(TABSTRING));
  53.       else
  54.          PushChar(*buf);
  55.    
  56.       buf++;
  57.    }
  58. }
  59.  
  60.  
  61. //
  62. // PushChar -- pushes a character into the LineBuf.  Pushes line into
  63. // the line list if <cr> encountered, or LineBuf is full.  Discards
  64. // linefeeds.
  65. //
  66. VOID PushChar(char ch)
  67. {
  68.    if (LineLen < sizeof(LineBuf))
  69.       if (ch != LF)
  70.          if (ch != CR)
  71.             LineBuf[LineLen++] = ch;
  72.          else;                         // discard carriage returns
  73.       else                             // linefeeds signal newline
  74.          PushLine();
  75.    else
  76.       PushLine();
  77. }
  78.  
  79. //
  80. // PushLine -- pushes a text line into the accumulating list of text lines.
  81. //
  82. VOID PushLine(VOID)
  83. {
  84.    LINEITEM *p;
  85.  
  86.    if (p = calloc(1, sizeof(LINEITEM)))
  87.    {
  88.       if (p->sztext = malloc(LineLen))
  89.       {
  90.          strncpy(p->sztext, LineBuf, p->len = LineLen);
  91.          pLast->next = p;
  92.          pLast = p;
  93.          LineLen = 0;
  94.          LineCount++;
  95.       }
  96.    }
  97. }
  98.  
  99. //
  100. // FreeDisplayList -- frees the display list, leaves a null list.
  101. //
  102. VOID FreeDisplayList(VOID)
  103. {
  104.    FreeLineList(pFirst);
  105.    pFirst = NULL;
  106.    LineCount = 0;
  107. }
  108.  
  109. //
  110. // FreeLineList -- frees a LINEITEM list
  111. //
  112. VOID FreeLineList(LINEITEM *pLine)
  113. {
  114.    if (pLine)
  115.    {
  116.       FreeLineList(pLine->next);
  117.       FreeLine(pLine);
  118.    }
  119. }
  120.  
  121. //
  122. // FreeLine -- frees a LINEITEM
  123. //
  124. VOID FreeLine(LINEITEM *pLine)
  125. {
  126.    free(pLine->sztext);
  127.    free(pLine);
  128. }
  129.  
  130. //
  131. // OpenDisplayList -- initializes a display list.  Call before using
  132. // PushChar() to build the list.
  133. //
  134. BOOL OpenDisplayList(VOID)
  135. {
  136.    LineCount = LineLen = 0;
  137.  
  138.    // initialize list with a dummy LINEITEM
  139.    if (pLast = pFirst = calloc(1, sizeof(LINEITEM)))
  140.       return TRUE;
  141.  
  142.    return FALSE;  // failure
  143. }
  144.  
  145. //
  146. // CloseDisplayList -- pushes final LINEITEM (if any) onto list, and
  147. // releases dummy list header.  Call after last PushChar() to complete
  148. // the display list.
  149. //
  150. VOID CloseDisplayList(VOID)
  151. {
  152.    LINEITEM *p = pFirst;
  153.  
  154.    if (LineLen > 0)
  155.       PushLine();
  156.  
  157.    pFirst = pFirst->next;
  158.    FreeLine(p);
  159. }
  160.  
  161. //
  162. // GetDispList -- retrieves the display list.
  163. //
  164. VOID GetDisplayList(LINEITEM **ppLine, int *pNLines)
  165. {
  166.    *ppLine = pFirst;
  167.    *pNLines = LineCount;
  168. }
  169.